<template>
  <div class="nav-box" id="nav-box" v-loading="navLoading">
    <el-menu :collapse="isCollapse"
             unique-opened
             :default-active="activeIndex"> <!--:default-active="activeIndex"-->
      <template v-for="(item, index) in navList">
        <el-submenu :index="item.menuName"
                    v-if="item.children"
                    :key="index">
          <template slot="title">
            <i :class="item.icon"></i>
            <span slot="title">{{item.menuName}}</span>
          </template>

          <div v-for="(itemChild, indexNum) in item.children"
               :key="indexNum">
            <div v-if="itemChild.children">
              <el-submenu :index="itemChild.menuName">
                <template slot="title">
                  <i :class="itemChild.icon"></i>
                  <span slot="title">{{itemChild.menuName}}</span>
                </template>
                <el-menu-item :index="itemG.menuName"
                              v-for="(itemG, indexG) in itemChild.children" :key="indexG"
                              @click="handleToPage(itemG)">
                  <i :class="itemG.icon"></i>
                  <span slot="title">{{itemG.menuName}}</span>
                </el-menu-item>
              </el-submenu>
            </div>
            <div v-else>
              <el-menu-item :index="itemChild.menuName"
                            @click="handleToPage(itemChild)">
                <i :class="itemChild.icon"></i>
                <span slot="title">{{itemChild.menuName}}</span>
              </el-menu-item>
            </div>
          </div>

        </el-submenu>

        <el-menu-item :index="item.menuName"
                      @click="handleToPage(item)"
                      v-else-if="!item.children"
                      :key="index">
          <i :class="item.icon"></i>
          <span slot="title">{{item.menuName}}</span>
        </el-menu-item>

      </template>
    </el-menu>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { mapFields } from 'vuex-map-fields'
const newArr = []
const setActiveIndex = (arr, path) => {
  arr.map((item) => {
    if (item.children && item.children.length > 0) {
      setActiveIndex(item.children)
    } else {
      newArr.push(item)
    }
  })
  let active = ''
  newArr.map((item) => {
    if (item.path && path && (path.indexOf(item.path) > -1 || item.path.indexOf(path) > -1)) {
      active = item.menuName
    }
  })
  return active
}

export default {
  name: 'leftNav',
  props: {
    navList: {
      default: () => ([])
    },
    navLoading: {
      type: Boolean,
      default: false
    }
  },
  mounted () {
    /* setTimeout(() => {
      this.$nextTick(() => {
        console.log(document.getElementsByClassName('is-active')[1])
        document.getElementsByClassName('is-active')[1].scrollIntoView({
          behavior: 'smooth', // 平滑过渡
          block: 'start' // 上边框与视窗顶部平齐。默认值
        })
      })
    }, 100) */
  },
  computed: {
    ...mapFields('profile/navNarrow', {
      isCollapse: 'retrieved'
    }),
    activeIndex () {
      return this.navList ? setActiveIndex(this.navList, this.$route.path) : ''
    }
  },
  methods: {
    ...mapActions('profile/tags', ['addTags']),
    ...mapActions('profile/navNarrow', ['setNavNarrow']),
    handleToPage (item) {
      if (item.path) {
        this.addTags({
          path: item.path,
          name: item.menuName
        })
        this.setNavNarrow(false)
        this.$router.push(item.path)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  .nav-box {
    ::v-deep .el-menu {
      background: none;
      border-right: none;

      .el-submenu__title:hover, .el-menu-item:hover, .el-menu-item:focus {
        opacity: 0.8;
        background: none;
      }
    }
  }
</style>
